﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using Hyt.Core.Authentication;
using Hyt.Core.Security;

namespace Hyt.Core.Interceptors
{
    public class AuthInterceptor : StandardInterceptor  
    {
        /// <summary>
        /// 授权管理器
        /// </summary>
        public  IAuthManager AuthManager { get; set; }

        public IPrincipal Principal
        {
            set { _principal = value; }
        }

        private IPrincipal _principal;

        protected override void PreProceed(IInvocation invocation)
        {
            var datas=invocation.Method.GetCustomAttributes(typeof(AuthOperationAttribute),false);
            if (datas.Any())
            {
                var attr = (AuthOperationAttribute)datas[0];
                var result = AuthManager.Check(_principal, attr.Operation);
                if (!result.Status)
                {
                    throw new Exception(result.Message??"无权限");
                }
            }
            base.PreProceed(invocation);
        }

        protected override void PostProceed(IInvocation invocation)
        {
            base.PostProceed(invocation);
        }

        protected override void PerformProceed(IInvocation invocation)
        {
            base.PerformProceed(invocation);
        }
    }
}
